/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.PrivilegChecker;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.BusServiceRel;
import han.bifang.gwt.client.common.entity.MappingService;
import han.bifang.gwt.client.common.entity.Role;
import han.bifang.gwt.client.common.entity.ServiceQos;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.service.ServiceConstants;
import han.bifang.gwt.client.service.ServiceContentConstants;
import han.bifang.gwt.client.service.ServiceMessages;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * @Author Kent
 */
public class RemoteServiceDetailContent extends Content {
	private BifangGWTServiceAsync service = null;

	private GwtContext context = null;

	private VerticalPanel panel = null;

	private ServiceMessages cons;

	private MappingService ser;

	private ArrayList ds1 = null;
	private String id = null;

	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}

			ArrayList ds = (ArrayList) res.getResponse();

			ser = (MappingService) ds.get(0);
			ArrayList<ServiceQos> qos = (ArrayList<ServiceQos>) ds.get(1);
			ArrayList<BusServiceRel> busR = (ArrayList<BusServiceRel>) ds
					.get(2);
			String resourceId = (String) ds.get(3);
			ArrayList<Role> rl = (ArrayList<Role>) ds.get(4);

			TabPanel tab = new TabPanel();
			tab.add(new RemoteServiceBasicContent(cons, service, context, ser),
					cons.basic());
			tab
					.add(new RemoteServiceConfigContent(cons, service, context,
							qos), cons.config());
			tab.add(new RemoteServiceBusBindingContent(cons, service, context,
					ser.getDomainId(), busR), cons.bus());
			tab.add(new ServiceRoleRefContent(cons, service, context,
					resourceId, rl), cons.security());

			tab.selectTab(0);
			tab.setWidth("100%");
			panel.add(tab);
			panel.add(new HTML("&nbsp;"));

			HorizontalPanel h1 = new HorizontalPanel();

			Button b = new Button(cons.importVersion());
			b.addClickListener(new ClickListener() {

				public void onClick(Widget sender) {
					context.put(ServiceConstants.SERVICE, ser);

					listener
							.onContentChanged(
									ServiceContentConstants.REMOTE_SERVICE_ROOT,
									ServiceContentConstants.IMPORT_REMOTE_SERVICE_VERSION,
									null);
				}

			});
			if (PrivilegChecker.hasPrivilege(context, null)) {
				h1.add(b);
				h1.add(new HTML("&nbsp;&nbsp;"));
			}
			Button b1 = new Button(cons.remove());
			b1.addClickListener(new ClickListener() {

				public void onClick(Widget sender) {
					// HashMap p = new HashMap();
					// p.put("id", ser.getId());
					// p.put("code", ser.getServiceCode());
					listener.onContentChanged(
							ServiceContentConstants.SERVICE_DETAIL,
							ServiceContentConstants.REMOVE_SERVICE, null);

				}

			});
			if (PrivilegChecker.hasPrivilege(context, null))
				h1.add(b1);
			h1.add(new HTML("&nbsp;&nbsp;"));

			Button b2 = new Button(cons.ret());
			b2.addClickListener(new ClickListener() {

				public void onClick(Widget sender) {
					// HashMap p = new HashMap();
					// p.put("id", ser.getId());
					// p.put("code", ser.getServiceCode());

					listener.onContentChanged(
							ServiceContentConstants.SERVICE_DETAIL,
							ServiceContentConstants.SEARCH_RESULT, null);

				}

			});
			h1.add(b2);

			panel.add(h1);

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	public RemoteServiceDetailContent(ServiceMessages cons,
			BifangGWTServiceAsync service, GwtContext ctx, ArrayList ds,
			String id) {
		this.cons = cons;
		this.service = service;
		this.context = ctx;
		this.ds1 = ds;
		this.id = id;
		panel = new VerticalPanel();
		panel.setWidth("100%");

		initWidget(panel);

	}

	public void onLoad() {
		if (ds1 != null) {
			ser = (MappingService) ds1.get(0);
			ArrayList<ServiceQos> qos = (ArrayList<ServiceQos>) ds1.get(1);
			ArrayList<BusServiceRel> r = (ArrayList<BusServiceRel>) ds1.get(2);
			String resourceId = (String) ds1.get(3);
			ArrayList<Role> rl = (ArrayList<Role>) ds1.get(4);

			context.put(ServiceConstants.MAPPING_SERVICE, ser);
			TabPanel tab = new TabPanel();
			tab.add(new RemoteServiceBasicContent(cons, service, context, ser),
					cons.basic());
			tab
					.add(new RemoteServiceConfigContent(cons, service, context,
							qos), cons.config());
			tab.add(new RemoteServiceBusBindingContent(cons, service, context,
					ser.getDomainId(), r), cons.bus());
			tab.add(new ServiceRoleRefContent(cons, service, context,
					resourceId, rl), cons.security());

			tab.selectTab(0);
			tab.setWidth("100%");
			panel.add(tab);
			panel.add(new HTML("&nbsp;"));

			HorizontalPanel h1 = new HorizontalPanel();

			Button b = new Button(cons.importVersion());
			b.addClickListener(new ClickListener() {

				public void onClick(Widget sender) {

					listener
							.onContentChanged(
									ServiceContentConstants.REMOTE_SERVICE_ROOT,
									ServiceContentConstants.IMPORT_REMOTE_SERVICE_VERSION,
									null);
				}

			});
			if (PrivilegChecker.hasPrivilege(context, null)) {
				h1.add(b);
				h1.add(new HTML("&nbsp;&nbsp;"));
			}
			Button b1 = new Button(cons.remove());
			b1.addClickListener(new ClickListener() {

				public void onClick(Widget sender) {
					HashMap p = new HashMap();
					p.put("s", ser);
					// p.put("code", ser.getServiceCode());
					listener.onContentChanged(
							ServiceContentConstants.REMOTE_SERVICE_ROOT,
							ServiceContentConstants.REMOVE_REMOTE_SERVICE, p);

				}

			});
			if (PrivilegChecker.hasPrivilege(context, null))
				h1.add(b1);
			h1.add(new HTML("&nbsp;&nbsp;"));

			Button b2 = new Button(cons.ret());
			b2.addClickListener(new ClickListener() {

				public void onClick(Widget sender) {
					// HashMap p = new HashMap();
					// p.put("id", ser.getId());
					// p.put("code", ser.getServiceCode());

					listener.onContentChanged(
							ServiceContentConstants.REMOTE_SERVICE_ROOT,
							ServiceContentConstants.SEARCH_RESULT, null);

				}

			});
			h1.add(b2);

			panel.add(h1);
		} else {
			RequestObject req = new RequestObject();
			req.setRequest(id);
			req
					.setAction("han.bifang.gwt.server.action.service.GetRemoteServiceDetailAction");
			service.call(req, cb);
		}
	}

}
